package com.jinandi.app.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jinandi.app.dao.AuthoritAndResDao;
import com.jinandi.app.dao.AuthorityDao;
import com.jinandi.app.dao.ResourceDao;
import com.jinandi.app.domain.Authorit;
import com.jinandi.app.domain.AuthoritAndRes;
import com.jinandi.app.domain.Resource;

@Service
public class AuthoritAndResService extends BaseService{
	
	private AuthoritAndResDao andResDao;
	private ResourceDao resourceDao;
	private AuthorityDao authorityDao;
	
	@Autowired
	public void setAuthorityDao(AuthorityDao authorityDao) {
		this.authorityDao = authorityDao;
	}

	@Autowired
	public void setResourceDao(ResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}

	@Autowired
	public void setAndResDao(AuthoritAndResDao andResDao) {
		this.andResDao = andResDao;
	}
	
	/**
	 * <修改角色拥有的资源>
	 * @param aid
	 * @param ids
	 * @author CGC 
	 * 创建日期:2013-5-9 下午1:42:25
	 */
	@Transactional
	public synchronized void modifyAuthoritAndRes(Long aid, String[] ids) {
		Authorit authorit = authorityDao.findAuthoritById(aid);
		andResDao.deleteByAuthorit(aid);
		for (String id : ids) {
			AuthoritAndRes authoritAndRes = new AuthoritAndRes();
			Resource resource = resourceDao.findResById(Long.valueOf(id));
			authoritAndRes.setAuthorit(authorit);
			authoritAndRes.setResource(resource);
			andResDao.save(authoritAndRes);
		}
	}
	
}
